### Packages
import cv2
import numpy as np
import matplotlib.pyplot as plt
### 출력 영상 크기
plt.rcParams["figure.figsize"] = (16,9)
### 한글 표시
plt.rcParams['font.family'] = "Gulim" # 'AppleGothic' in mac
### 영상 읽기
img_file = r'D:\image\bp2.jpg'
img_bgr = cv2.imread(img_file)
### 영상 출력
plt.imshow(cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB))
plt.show()
### Package
from distutils.sysconfig import get_python_lib
### 캐스케이드 파일
cascade_file = get_python_lib() + r"\cv2\data\haarcascade_frontalface_alt2.xml"
cascade_file
'C:\\ProgramData\\Anaconda3\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_alt2.xml'
### 캐스케이드 파일 읽기
cascade = cv2.CascadeClassifier(cascade_file)
### 그레이스케일로 변환
img_gs = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)
### 얼굴 검출
face_list = cascade.detectMultiScale(img_gs, minSize = (30, 30))
### 얼굴 검출 좌표 - [x, y, w, h]
face_list
array([[214, 74, 71, 71],
[376, 64, 65, 65],
[141, 181, 48, 48],
[ 35, 64, 72, 72]])
### 얼굴 좌표 확인
if len(face_list) > 0:
### 이미지 복사
img_out = img_bgr.copy()
### 색상 - red
color = (0, 0, 255)
### 얼굴 영역 표시
for face in face_list :
x, y, w, h = face
cv2.rectangle(img_out, (x, y), (x+w, y+h), color, thickness = 3)
### RGB로 변환 후 이미지 출력
plt.imshow(cv2.cvtColor(img_out, cv2.COLOR_BGR2RGB))
plt.show()
else:
print("no face")
### 모자이크 처리 함수
def fn_mosaic (img, rect, size = 9) :
x1, y1, x2, y2 = rect
w = x2 - x1
h = y2 - y1
img_small = cv2.resize(img[y1:y2, x1:x2], (size, size))
img[y1:y2, x1:x2] = cv2.resize(img_small, (w, h),
interpolation = cv2.INTER_AREA)
return img
### 이미지 복사
img_out = img_bgr.copy()
### 모자이크 처리
for x, y, w, h in face_list :
### 이미지 삽입
img_out = fn_mosaic(img_out, (x, y, x+w, y+h))
### RGB로 변환 후 이미지 출력
plt.imshow(cv2.cvtColor(img_out, cv2.COLOR_BGR2RGB))
plt.show()
### Packages
import cv2
import numpy as np
import matplotlib.pyplot as plt
import dlib
### 얼굴 검출기 - HOG
detector_hog = dlib.get_frontal_face_detector()
### 얼굴 검출 - RGB 영상 사용
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
dlib_rects = detector_hog(img_rgb, 1)
### 얼굴 검출 좌표 - [(x1, y1) (x2, y2)]
dlib_rects
rectangles[[(218, 80) (280, 142)], [(38, 73) (100, 135)], [(377, 73) (439, 135)], [(501, 66) (563, 129)]]
### 얼굴 좌표 확인
if len(face_list) > 0:
### 이미지 복사
img_out = img_bgr.copy()
### 색상 - green
color = (0, 255, 0)
### 얼굴 영역 표시
for dlib_rect in dlib_rects:
rect_lt = (dlib_rect.left(), dlib_rect.top())
rect_rb = (dlib_rect.right(), dlib_rect.bottom())
cv2.rectangle(img_out, rect_lt, rect_rb, color, 2,
lineType=cv2.LINE_AA)
### RGB로 변환 후 이미지 출력
plt.imshow(cv2.cvtColor(img_out, cv2.COLOR_BGR2RGB))
plt.show()
else:
print("no face")
### 이미지 복사
img_out = img_bgr.copy()
### 모자이크 처리
for dlib_rect in dlib_rects:
### 이미지 삽입
x1 = dlib_rect.left()
y1 = dlib_rect.top()
x2 = dlib_rect.right()
y2 = dlib_rect.bottom()
img_out = fn_mosaic(img_out, (x1, y1, x2, y2))
### RGB로 변환 후 이미지 출력
plt.imshow(cv2.cvtColor(img_out, cv2.COLOR_BGR2RGB))
plt.show()
### 검출기
detector_hog = dlib.get_frontal_face_detector()
### 카메라 연결
cap = cv2.VideoCapture(0)
### 동영상 출력
while cv2.waitKey(1) < 0:
# Capture frame-by-frame
ret, frame = cap.read()
if ret:
### 얼굴 검출
dlib_rects = detector_hog(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), 1)
### 모자이크 처리
for dlib_rect in dlib_rects:
### 이미지 삽입
x1 = dlib_rect.left()
y1 = dlib_rect.top()
x2 = dlib_rect.right()
y2 = dlib_rect.bottom()
frame = fn_mosaic(frame, (x1, y1, x2, y2))
### Frame 출력
cv2.imshow("VideoFrame", frame)
else:
print("Can't receive frame. Exiting...")
break
### When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
### 영상 읽기
img_file = r'D:\image\bp2.jpg'
img_bgr = cv2.imread(img_file)
### 얼굴 검출기 - HOG
detector_hog = dlib.get_frontal_face_detector()
### 얼굴 검출 - RGB 영상 사용
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
dlib_rects = detector_hog(img_rgb, 1)
### 검출기
landmark_predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
### face landmark
list_landmarks = []
### face landmark 검출
for dlib_rect in dlib_rects:
points = landmark_predictor(img_rgb, dlib_rect)
### face landmark 좌표 저장
list_points = list(map(lambda p: (p.x, p.y), points.parts()))
list_landmarks.append(list_points)
### face landmark 확인
len(list_landmarks), list_landmarks[0][:6]
(4, [(217, 99), (218, 107), (220, 114), (221, 122), (225, 129), (230, 135)])
### 이미지 복사
img_out = img_bgr.copy()
### face landmark 표시
for landmark in list_landmarks:
for point in landmark:
cv2.circle(img_out, point, 2, (255, 255, 255), -1)
### 영상 출력
plt.imshow(cv2.cvtColor(img_out, cv2.COLOR_BGR2RGB))
plt.show()
### 검출기
detector_hog = dlib.get_frontal_face_detector()
landmark_predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
### 카메라 연결
cap = cv2.VideoCapture(0)
### 동영상 출력
while cv2.waitKey(1) < 0:
# Capture frame-by-frame
ret, frame = cap.read()
if ret:
### 얼굴 검출
dlib_rects = detector_hog(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), 1)
### face landmark
list_landmarks = []
### face landmark 검출
for dlib_rect in dlib_rects:
points = landmark_predictor(frame, dlib_rect)
### face landmark 좌표 저장
list_points = list(map(lambda p: (p.x, p.y), points.parts()))
list_landmarks.append(list_points)
### face landmark 표시
for landmark in list_landmarks:
for point in landmark:
cv2.circle(frame, point, 2, (255, 255, 255), -1)
### Frame 출력
cv2.imshow("VideoFrame", frame)
else:
print("Can't receive frame. Exiting...")
break
### When everything done, release the capture
cap.release()
cv2.destroyAllWindows()